package Dbase;

import model.userData;

import java.sql.*;
import java.util.ArrayList;

public class dataBase {
    Connection connect = null;
    public dataBase() throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.cj.jdbc.Driver");
        this.connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/loserver?useSSL=false&serverTimezone=UTC",
                "root","041205");
        System.out.println(connect);
    }
    public void showConnected(){
        if(connect != null){
            System.out.println("连接成功");
        }
        else{
            System.out.println("连接失败");
        }
    }
    public void close() throws SQLException {
        connect.close();
    }
    public void insert(String name,String password) throws SQLException {
        PreparedStatement PS = connect.prepareStatement("insert into loserver.login value(null,?,?) ");
        PS.setString(1,name);
        PS.setString(2,password);
        PS.execute();
    }
    public ArrayList<userData> getAllUser() throws SQLException {
        ArrayList<userData> userlist = new ArrayList<userData>();
        PreparedStatement PS = connect.prepareStatement("select * from loserver.login");
        PS.execute();
        ResultSet resultSet = PS.executeQuery();
        while(resultSet.next()){
            int my_id = resultSet.getInt("id");
            String my_name = resultSet.getString("name");
            String my_pwd = resultSet.getString("password");
            userlist.add(new userData(my_id,my_name,my_pwd));
        }
        return userlist;
    }
    public int getId(String name) throws SQLException {
        PreparedStatement PS = connect.prepareStatement("select id from loserver.login where name=?");
        PS.setString(1,name);
        PS.execute();
        ResultSet resultSet = PS.executeQuery();
        if(resultSet.next()){
            int my_id = resultSet.getInt("id");
            return my_id;
        }
        else{
            return -1;
        }
    }
    public userData getUser(int id) throws SQLException {
        PreparedStatement PS = connect.prepareStatement("select * from loserver.login where id=?");
        PS.setInt(1,id);
        PS.execute();
        ResultSet resultSet = PS.executeQuery();
        if (resultSet.next()) {
            int my_id = resultSet.getInt("id");
            String my_name = resultSet.getString("name");
            String my_pwd = resultSet.getString("password");
            return new userData(my_id,my_name,my_pwd);
        }
        else {
            return null;
        }
    }
    public userData Login(int id,String pwd) throws SQLException {
        PreparedStatement PS = connect.prepareStatement("select loserver.login.password from loserver.login where id=?");
        PS.setInt(1,id);
        PS.execute();
        ResultSet resultSet = PS.executeQuery();
        if(resultSet.next()){
            String my_pwd = resultSet.getString("password");
            if(pwd.equals(my_pwd)){
                return getUser(id);
            }
            else{
                return null;
            }
        }
        else {
            return null;
        }
    }
}
